
#include "NoteBox.h"
#include <FL/fl_ask.H>


NoteBox::NoteBox (int *glObjectIdIndexIn)
{
    id = *glObjectIdIndexIn;
    idSizer = id + 1;
    *glObjectIdIndexIn += 2;  //because we have two glLoadName ids

    x = 1;
    y = 1;
    w = 1;
    h = 1;

    r = 0.6;
    g = 0.6;
    b = 0.0;
    isOn = false;
    draggingSize = false;
    dragging = false;

    pluginId = 0; //temp for testing
//    slaveId = -1;
}

NoteBox::NoteBox ()
{
    x = 1;
    y = 1;
    w = 1;
    h = 1;

    r = 0.6;
    g = 0.6;
    b = 0.0;
    isOn = false;
    draggingSize = false;
    dragging = false;

    pluginId = 0; //temp for testing
}

//return true to indicate we want to delete this note
bool NoteBox::event( int event, int button, double xGl, double yGl, int glObjectIdIndexIn)
{
    if (glObjectIdIndexIn != -1 &&
        glObjectIdIndexIn != id &&
        glObjectIdIndexIn != idSizer )
            return false;

    if ( event == FL_RELEASE )
    {
        draggingSize = false;
        dragging = false;
    }

    if ( event == FL_PUSH )
    {
        mouseClickGlX = xGl - x;
        mouseClickGlY = yGl - y;
        if (button == 3)
            return true; //return true to indicate we want to delete this note
    }

    if ( glObjectIdIndexIn == id )
    {
        dragging = true;
        draggingSize = false;
        if (button == 2)
            toggle();
    }

    else if ( glObjectIdIndexIn == idSizer )
    {
        draggingSize = true;
        dragging = false;
    }

    if ( event == FL_DRAG && draggingSize )
    {
        w = xGl - x;
        if (w<=0) w = 1;
    }

    if ( event == FL_DRAG && dragging )
    {
        x = (xGl - (mouseClickGlX));
        y = (GLint)yGl;
    }
    return false;
}

void NoteBox::toggle()
{
    if (pluginId == 0)
        pluginId = 1;
    else
        pluginId = 0;
}

void NoteBox::draw()
{
   /* /
    glPointSize(3);

	// draw CV's
	glBegin(GL_POINTS);
	for(int i=0;i!=NUM_POINTS;++i) {
		glVertex3fv( spline.points[i] );
	}
	glEnd();
*/
    // (Type of blending) Set the blending

    glLoadName(id);
    glBegin(GL_QUADS);
    if (isOn)
        glColor4f(r, g, b, 0.6);
    else
        glColor4f(0.1, 0.4, 0.7, 0.6);

    glVertex3f(x, y, 0); glVertex3f(x, y+h, 0);
    glVertex3f(x+w, y+h, 0); glVertex3f(x+w, y, 0);
    glEnd();

    glLoadName(idSizer);
    glLineWidth(2);
    glBegin(GL_LINE_STRIP);

    if (isOn)
        glColor3f(0, 0, 1);
    else
        glColor3f(0.1, 0.1, 0.3);

    glVertex3f(x+w, y, 0);
    glVertex3f(x+w, y+h, 0);
    glEnd();
/*
    char s[255];
    sprintf(s, "nId=%d", id);
    glColor3f(1, 0, 1);
    gl_font(FL_COURIER, 10);
    gl_draw(s, (GLint)x, y);
*/
}


